package com.mypage.view.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.view.activity.TaskManagementActivty;
import com.cloudcc.mobile.weight.CustomProgressDialog;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.litesuits.android.async.Log;
import com.mypage.utils.NetworkDetector;

import org.json.JSONException;
import org.json.JSONObject;

public class RefuseActivityDialog extends Activity implements OnClickListener {

	private Button mCancel;
	private Button mSure;
	private RelativeLayout mLayout;
	private EditText mContent;
	private String content;
	private String workItemId;
	private CustomProgressDialog progressDialog;
	private String isNew = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.refuse_activity_dialog);
		progressDialog = CustomProgressDialog.createDialog(this);
		Intent intent = getIntent();
		workItemId = intent.getStringExtra("workItemId");
		isRequired = intent.getStringExtra("isRequired");
		isNew = intent.getStringExtra("weizhi");
		initViews();
		initListener();
	}

	/**
	 * dialog开始显示
	 */
	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(context);
			progressDialog.setCancelable(true);
			progressDialog.setCanceledOnTouchOutside(false);
			// progressDialog.setMessage("正在加载中...");
		}
		progressDialog.show();
	}

	/**
	 * 停止dialog
	 */
	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	private void initViews() {
		// TODO Auto-generated method stub
		mContent = (EditText) findViewById(R.id.editTextContent);
		mCancel = (Button) findViewById(R.id.quxiaocanle);
		mSure = (Button) findViewById(R.id.quedingsure);
		mLayout = (RelativeLayout) findViewById(R.id.layoutLine);
		mLayout.setVisibility(View.VISIBLE);
		mSure.setText("拒绝");

	}

	private void initListener() {
		// TODO Auto-generated method stub
		mCancel.setOnClickListener(this);
		mSure.setOnClickListener(this);

	}
	private String isRequired = "false";
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {

		case R.id.quxiaocanle:
			finish();
			// Toast.makeText(RefuseActivityDialog.this, "取消",
			// Toast.LENGTH_SHORT)
			// .show();
			break;

		case R.id.quedingsure:
			content = mContent.getText().toString().trim();
			if("true".equals(isRequired)){
				if(content.length()<=0){
					Toast.makeText(RefuseActivityDialog.this, getResources().getString(R.string.bbixueizhu),
							Toast.LENGTH_SHORT).show();
					return;
				}
			}
			// if (content.length() == 0) {
			// Toast.makeText(RefuseActivityDialog.this, "批注为空...",
			// Toast.LENGTH_SHORT).show();
			// } else
			if(content.length()>500){
				Toast.makeText(RefuseActivityDialog.this, "不可以超过500字",
						Toast.LENGTH_SHORT).show();
				return;
			}
			if (!NetworkDetector.detect(RefuseActivityDialog.this)) {

				Toast.makeText(RefuseActivityDialog.this, "当前网络不可用....",
						Toast.LENGTH_SHORT).show();

			} else {
				if(!"xiangqing".equals(isNew)){
					mSure.setText("处理中");
					setDataNetWorkNew(content);
					progressDialog.setCanceledOnTouchOutside(false);
					startProgressDialog();
				}else {
					mSure.setText("处理中");
					setDataNetWork(content);
					progressDialog.setCanceledOnTouchOutside(false);
					startProgressDialog();
				}

			}

			break;

		default:
			break;
		}

	}

	Context context;

	public void setDataNetWork(String content) {
		// 拒绝
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("binding", RunTimeManager.getInstance()
				.getServerBinding());
		params.addBodyParameter("serviceName", "doRejected");
		params.addBodyParameter("data", createJson());
		Log.d("-------------------------", "//workItemId=" + workItemId
				+ "//Approved=" + "Approved" + "//content=" + content);

		utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {

						// TODO Auto-generated method stub
						stopProgressDialog();
						Intent intent = new Intent(RefuseActivityDialog.this,
								TaskManagementActivty.class);
//						if(workItemId.length()>30) {
//							intent.putExtra("chongxin", "chongxinmore");
//						}else{
						intent.putExtra("chongxinshibai","chongxin");
//						}
						startActivity(intent);
						sendBroadcast(new Intent().setAction("finish"));
						finish();
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String result = arg0.result;
						JSONObject obj = new JSONObject();

						Log.d("-------------------------", "获取数据成功" + result);
						sendBroadcast(new Intent().setAction("taskfinish"));
						stopProgressDialog();
						Intent intent = new Intent(RefuseActivityDialog.this,
								TaskManagementActivty.class);
						TaskManagementActivty.instance.finish();
						try {
							ApprovalPendingActivity.instance.finish();
						} catch (Exception e) {
							e.printStackTrace();
						}
						intent.putExtra("taskfinish","xiangqingpizhun");
						startActivity(intent);

						sendBroadcast(new Intent().setAction("finish"));
						finish();

					}
				});
	}
	public String createJson() {
		JSONObject object = new JSONObject();
		try {

//			params.addBodyParameter("workItemId", workItemId);
//			params.addBodyParameter("comments", content);
			object.put("workItemId", workItemId);
			object.put("comments", content);
			// Log.d("/////////////////", object.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object.toString();

	}



	public void setDataNetWorkNew(String content) {
		// 拒绝
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("userId",
				UserManager.getManager().getUser().userId);
		params.addBodyParameter("binding", RunTimeManager.getInstance()
				.getServerBinding());
		params.addBodyParameter("serviceName", "ajaxProcessMassThread");
		params.addBodyParameter("workItemId", workItemId);
		params.addBodyParameter("actionType", "Rejected");
		params.addBodyParameter("comments", content);
		Log.d("-------------------------", "//workItemId=" + workItemId
				+ "//Approved=" + "Approved" + "//content=" + content);

		utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {

						// TODO Auto-generated method stub
						stopProgressDialog();
						Intent intent = new Intent(RefuseActivityDialog.this,
								TaskManagementActivty.class);
//						if(workItemId.length()>30) {
//							intent.putExtra("chongxin", "chongxinmore");
//						}else{
						intent.putExtra("chongxinshibai","chongxin");
//						}
						startActivity(intent);
						sendBroadcast(new Intent().setAction("finish"));
						finish();
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String result = arg0.result;
						JSONObject obj = new JSONObject();

						Log.d("-------------------------", "获取数据成功" + result);
						sendBroadcast(new Intent().setAction("taskfinish"));
						stopProgressDialog();
						Intent intent = new Intent(RefuseActivityDialog.this,
								TaskManagementActivty.class);
						try {
							TaskManagementActivty.instance.finish();
						} catch (Exception e) {
							e.printStackTrace();
						}
						try{
							ApprovalPendingActivity.instance.finish();
						}catch (Exception e){

						}
						if(workItemId.length()>30) {
							intent.putExtra("zhuangtai", "jujue");
						}else {
							intent.putExtra("taskfinish","taskfinish");
						}
						startActivity(intent);

						sendBroadcast(new Intent().setAction("finish"));
						finish();

					}
				});

	}
}
